package com.vast.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vast.admin.annotation.UseToken;
import com.vast.admin.entity.Customer;
import com.vast.admin.entity.enums.QueryOrderEnum;
import com.vast.admin.service.CustomerService;
import com.vast.common.annotation.Log;
import com.vast.common.core.domain.entity.SysUser;
import com.vast.common.utils.SecurityUtils;
import com.vast.common.constant.MessageCode;
import com.vast.common.core.domain.Result;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(value = "客户相关接口", tags = { "客户管理模块" })
@RequestMapping("/manager/customer")
public class CustomerController {

    @Autowired
    private CustomerService service;

    @ApiOperation(value = "查询列表信息", notes = "通过条件查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "size", value = "分页条数", dataType = "int", defaultValue = "10", required = false),
            @ApiImplicitParam(paramType = "query", name = "current", value = "页码,起始值:1", dataType = "int", defaultValue = "1", required = false),
            @ApiImplicitParam(paramType = "query", name = "listMode", value = "是否集合模式:true不进行分页直接返回所有结果集", dataType = "boolean", defaultValue = "false", required = false),
            @ApiImplicitParam(paramType = "query", name = "queryItems", value = "查询条件,json集合", defaultValue = "account,EQ,13419519796;", dataType = "String", required = false),
            @ApiImplicitParam(paramType = "query", name = "orderField", value = "排序字段,多个','分隔", defaultValue = "create_time", dataType = "String",  required = false),
            @ApiImplicitParam(paramType = "query", name = "order", value = "排序类型: ASC/DESC", dataType = "QueryOrderEnum",  required = false)
    })
    @GetMapping("/page")
    public Result<IPage<Customer>> page(@ApiParam(hidden = true) Page<Customer> page, boolean listMode, String queryItems, String orderField, QueryOrderEnum order) {
        return new Result<IPage<Customer>>(MessageCode.SUCCESS, "获取成功", service.queryList(page, listMode, queryItems, orderField,order));

    }

    @ApiOperation(value = "添加或更新", notes = "添加或更新")
    @UseToken
    @PostMapping("/addOrUpdate")
    @Log(title = "添加或更新企业")
    public Result<Customer> addOrUpdate(@RequestBody @ApiParam(name="Customer",value="Customer信息",required=true) Customer customer) {
        if(customer != null) {
			SysUser manager = SecurityUtils.getLoginUser().getUser();
            boolean flag  = service.addOrUpdate(manager, customer);
            if(flag){
                return new Result<Customer>(MessageCode.SUCCESS, "成功", customer);
            }else{
                return new Result<Customer>(MessageCode.ERROR, "失败");
            }
        }else {
            return new Result<Customer>(MessageCode.PARAM_ERROR, "参数错误");
        }
    }

    @ApiOperation(value = "通过id获取信息", notes = "通过id获取信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token", dataType = "String", required = true)})
    @GetMapping("/info/{id}")
    @UseToken
    @Log(title = "通过id获取信息")
    public Result<Customer> info(@PathVariable("id") Long id) {
        if(id != null && id.longValue() > 0){
			SysUser manager = SecurityUtils.getLoginUser().getUser();
            Customer bean = service.info(manager,id);
            if(bean != null){
                return new Result<Customer>(MessageCode.SUCCESS, "获取成功", bean);
            }else{
                return new Result<Customer>(MessageCode.ERROR, "获取失败");
            }
        }
        return new Result<Customer>(MessageCode.PARAM_ERROR, "id不能为空");
    }

    @ApiOperation(value = "删除", notes = "删除")
    @UseToken
    @Log(title = "删除")
    @DeleteMapping("/delete")
    public Result<Boolean> delete(@RequestBody @ApiParam(name="ids",value="数据id数组,以','分隔",required=true) Long[] ids) {
        if(ids != null) {
			SysUser manager = SecurityUtils.getLoginUser().getUser();
            boolean flag  = service.delete(manager, ids);
            if(flag){
                return new Result<Boolean>(MessageCode.SUCCESS, "删除成功");
            }else{
                return new Result<Boolean>(MessageCode.ERROR, "删除失败");
            }
        }else {
            return new Result<Boolean>(MessageCode.PARAM_ERROR, "参数错误");
        }
    }

}
